package ning.zhi.ze.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.fragment.app.Fragment;


import com.google.gson.Gson;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.ArrayList;

import ning.zhi.ze.MyApp;
import ning.zhi.ze.R;
import ning.zhi.ze.TestBena;


public class HomeFragment extends Fragment {

    private AppCompatTextView mainTextView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        mainTextView = view.findViewById(R.id.mainTextView);

        mainTextView.setOnClickListener(view1 -> {
            ArrayList<TestBena> arrayList = new ArrayList<>();
            TestBena testBena = new TestBena();
            testBena.setName("张三");
            testBena.setAge("18");
            testBena.setOther("123123123123");
            arrayList.add(testBena);
            testBena = new TestBena();
            testBena.setName("张三");
            testBena.setAge("18");
            testBena.setOther("123123123123");
            arrayList.add(testBena);
            testBena = new TestBena();
            testBena.setName("张三");
            testBena.setAge("18");
            testBena.setOther("123123123123");
            arrayList.add(testBena);
            testBena = new TestBena();
            testBena.setName("张三");
            testBena.setAge("18");
            testBena.setOther("123123123123");
            arrayList.add(testBena);
            MyApp.get().getEditor().putString("test_data", new Gson().toJson(arrayList));
            MyApp.get().getEditor().apply();


            LiveEventBus.get("position").post(4);
        });

        initObserve();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d("TAGS", "fragment onresume 方法");
    }

    private void initObserve() {

        LiveEventBus.get("test", String.class).observe(this, s -> {
            mainTextView.setText(s);
        });

    }

}
